<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>React 状态管理终结篇 - 早起的年轻人</title>
    <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="早起的年轻人" /><meta name="description" content=" Unstated Next readme 的中文翻译
 前言 这个库的作者希望使用 React 内置 API ,直接实现状态管理的功能。看完这个库的说明后，没有想到代码可以这个玩。短短几行代码，仅仅使用 React Hooks ，就实现了状态管理的功能。
看完之后，第一想法就是翻译成中文，分享给其他人。提交 Pull Request 后，库作者将我的翻译合并了。同时作者欢迎将 README 翻译成其他语言，以下是全部翻译内容，不妥之处欢迎指正或 Pull Request.
" /><meta name="keywords" content="管理, Github, Vue, flutter, Go" />






<meta name="generator" content="Hugo 0.79.1 with even 4.0.0" />


<link rel="canonical" href="https://luckly.work/post/2019/2019-05-08-react-unstated-next/" />
<link href="/post/2019/2019-05-08-react-unstated-next/" rel="alternate" type="application/rss+xml" title="早起的年轻人" />
<link href="/post/2019/2019-05-08-react-unstated-next/" rel="feed" type="application/rss+xml" title="早起的年轻人" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<link href="/dist/even.06658218.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">
<link rel="stylesheet" href="/css/reset-even.css">


<meta property="og:title" content="React 状态管理终结篇" />
<meta property="og:description" content="
Unstated Next readme 的中文翻译

前言
这个库的作者希望使用 React 内置 API ,直接实现状态管理的功能。看完这个库的说明后，没有想到代码可以这个玩。短短几行代码，仅仅使用 React Hooks ，就实现了状态管理的功能。
看完之后，第一想法就是翻译成中文，分享给其他人。提交 Pull Request 后，库作者将我的翻译合并了。同时作者欢迎将 README 翻译成其他语言，以下是全部翻译内容，不妥之处欢迎指正或 Pull Request." />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://luckly.work/post/2019/2019-05-08-react-unstated-next/" />
<meta property="article:published_time" content="2019-05-08T20:09:06+08:00" />
<meta property="article:modified_time" content="2019-05-08T20:09:06+08:00" />
<meta itemprop="name" content="React 状态管理终结篇">
<meta itemprop="description" content="
Unstated Next readme 的中文翻译

前言
这个库的作者希望使用 React 内置 API ,直接实现状态管理的功能。看完这个库的说明后，没有想到代码可以这个玩。短短几行代码，仅仅使用 React Hooks ，就实现了状态管理的功能。
看完之后，第一想法就是翻译成中文，分享给其他人。提交 Pull Request 后，库作者将我的翻译合并了。同时作者欢迎将 README 翻译成其他语言，以下是全部翻译内容，不妥之处欢迎指正或 Pull Request.">
<meta itemprop="datePublished" content="2019-05-08T20:09:06+08:00" />
<meta itemprop="dateModified" content="2019-05-08T20:09:06+08:00" />
<meta itemprop="wordCount" content="3061">



<meta itemprop="keywords" content="React,State Management," />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="React 状态管理终结篇"/>
<meta name="twitter:description" content="
Unstated Next readme 的中文翻译

前言
这个库的作者希望使用 React 内置 API ,直接实现状态管理的功能。看完这个库的说明后，没有想到代码可以这个玩。短短几行代码，仅仅使用 React Hooks ，就实现了状态管理的功能。
看完之后，第一想法就是翻译成中文，分享给其他人。提交 Pull Request 后，库作者将我的翻译合并了。同时作者欢迎将 README 翻译成其他语言，以下是全部翻译内容，不妥之处欢迎指正或 Pull Request."/>

</head>
<body>
<div id="mobile-navbar" class="mobile-navbar">
    <div class="mobile-header-logo">
        <a href="/" class="logo">早起的年轻人</a>
    </div>
    <div class="mobile-navbar-icon">
        <span></span>
        <span></span>
        <span></span>
    </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
    <ul class="mobile-menu-list">
        <a href="/">
            <li class="mobile-menu-item" title="首页">首页</li>
        </a>
        <a href="/post/">
            <li class="mobile-menu-item" title="归档">归档</li>
        </a>
        <a href="/categories/">
            <li class="mobile-menu-item" title="分类">分类</li>
        </a>
        <a href="/tags/">
            <li class="mobile-menu-item" title="标签">标签</li>
        </a>
        <a href="/about/">
            <li class="mobile-menu-item" title="关于我">关于我</li>
        </a>
        <a href="/friend/">
            <li class="mobile-menu-item" title="友链">友链</li>
        </a>
        <a href="https://github.com/ITmxs/">
            <li class="mobile-menu-item" title="阅读清单">阅读清单</li>
        </a>
        
    </ul>
    <form class="search-form" style="position: absolute;">
    <input name="search" type="search" style="height: 28px;padding-left: 30px;"/>
    <input type="image" src="/img/search.svg" alt="Search" style="position: absolute;left: 6px;top: 6px;"/>
</form>


</nav>

<div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/%20/" class="logo">早起的年轻人</a>
</div>

<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
      <a class="menu-item-link" href="/">首页</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/post/">归档</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/categories/">分类</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/tags/">标签</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/about/">关于我</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/friend/">友链</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="https://github.com/ITmxs/">阅读清单</a>
    </li>
  </ul>
</nav>
        <form class="search-form" style="position: absolute;">
    <input name="search" type="search" style="height: 28px;padding-left: 30px;"/>
    <input type="image" src="/img/search.svg" alt="Search" style="position: absolute;left: 6px;top: 6px;"/>
</form>



    </header>


    <main id="main" class="main">
        <div class="content-wrapper">
            <div id="content" class="content">
                <article class="post">
  
  <header class="post-header">
    <h1 class="post-title">React 状态管理终结篇</h1>

    <div class="post-meta">
      <span class="post-time"> 2019-05-08 20:09 </span>
      <div class="post-category">
        <a href="/categories/%E6%8A%80%E6%9C%AF/"> 技术 </a>
        </div>
      <span class="more-meta"> 约 3061 字 </span>
      <span class="more-meta"> 预计阅读 7 分钟 </span>
      <span id="busuanzi_container_page_pv" class="more-meta"> <span id="busuanzi_value_page_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次阅读 </span>
    </div>
  </header>

  <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#前言">前言</a></li>
        <li><a href="#unstated-next">Unstated Next</a>
          <ul>
            <li><a href="#查看-unstated-迁移手册-rarre4bb8e-unstated-e8bf81e7a7bb"><a href="#%E4%BB%8E-unstated-%E8%BF%81%E7%A7%BB">查看 Unstated 迁移手册 →</a></a></li>
          </ul>
        </li>
        <li><a href="#安装">安装</a></li>
        <li><a href="#example">Example</a></li>
        <li><a href="#api">API</a>
          <ul>
            <li><a href="#createcontainerusehook"><code>createContainer(useHook)</code></a></li>
            <li><a href="#containerprovider"><code>&lt;Container.Provider&gt;</code></a></li>
            <li><a href="#containerusecontainer"><code>Container.useContainer()</code></a></li>
            <li><a href="#usecontainercontainer"><code>useContainer(Container)</code></a></li>
          </ul>
        </li>
        <li><a href="#指南">指南</a></li>
        <li><a href="#提示">提示</a>
          <ul>
            <li><a href="#提示-1-组合-containers">提示 #1: 组合 Containers</a></li>
            <li><a href="#提示-2-保持-containers-很小">提示 #2: 保持 Containers 很小</a></li>
            <li><a href="#提示-3-优化组件">提示 #3: 优化组件</a></li>
          </ul>
        </li>
        <li><a href="#与-unstated-的关系">与 Unstated 的关系</a></li>
        <li><a href="#从-unstated-迁移">从 <code>unstated</code> 迁移</a></li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
  <div class="post-outdated">
    <div class="warn">
      <p>【注意】最后更新于 <span class="timeago" datetime="2019-05-08T20:09:06" title="May 8, 2019">May 8, 2019</span>，文中内容可能已过时，请谨慎使用。</p>
    </div>
  </div>
  <div class="post-content">
    <blockquote>
<p><a href="https://github.com/jamiebuilds/unstated-next">Unstated Next</a> readme 的中文翻译</p>
</blockquote>
<h2 id="前言">前言</h2>
<p>这个库的作者希望使用 React 内置 API ,直接实现状态管理的功能。看完这个库的说明后，没有想到代码可以这个玩。短短几行代码，仅仅使用 React Hooks ，就实现了状态管理的功能。</p>
<p>看完之后，第一想法就是翻译成中文，分享给其他人。提交 Pull Request 后，库作者将我的翻译合并了。同时作者欢迎将 README 翻译成其他语言，以下是全部翻译内容，不妥之处欢迎指正或 Pull Request.</p>
<h2 id="unstated-next">Unstated Next</h2>
<blockquote>
<p>永远不必再考虑 React 状态管理了，仅仅 200 字节的状态管理解决方案。</p>
</blockquote>
<ul>
<li><strong>React Hooks</strong> <em>React Hooks 用做你所有的状态管理。</em></li>
<li><strong>~200 bytes</strong> <em>min+gz.</em></li>
<li><strong>熟悉的 API</strong> <em>仅仅使用了 React，没有依赖第三方库。</em></li>
<li><strong>最小 API</strong> <em>只需 5 分钟学习。</em></li>
<li><strong>TypeScript 编写</strong> <em>推断代码更容易，易于编写 React 代码。</em></li>
</ul>
<p>但是，最重要的问题是：这比 Redux 更好吗？ 答案可能是。</p>
<ul>
<li><strong>它更小。</strong> <em>比 Redux 小 40 倍。</em></li>
<li><strong>它更快。</strong> <em>组件性能问题。</em></li>
<li><strong>它更容易学习。</strong> <em>你必须已经知道 React Hooks 和 Context 。只需使用它们，它们就会嗨起来。</em></li>
<li><strong>更容易集成。</strong> <em>一次集成一个组件，并且轻松与其他 React 库集成。</em></li>
<li><strong>它更容易测试。</strong> <em>测试 reducers 纯属浪费你的时间，这个库使你更容易测试 React 组件。</em></li>
<li><strong>它更容易进行类型检查。</strong> <em>旨在使你的大多数类型可推断。</em></li>
<li><strong>它是最小的。</strong> <em>仅仅使用了 React 。</em></li>
</ul>
<p>你自己看着办吧！</p>
<h3 id="查看-unstated-迁移手册-rarre4bb8e-unstated-e8bf81e7a7bb"><a href="#%E4%BB%8E-unstated-%E8%BF%81%E7%A7%BB">查看 Unstated 迁移手册 →</a></h3>
<h2 id="安装">安装</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-sh" data-lang="sh">npm install --save unstated-next
</code></pre></td></tr></table>
</div>
</div><h2 id="example">Example</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-js" data-lang="js"><span class="kr">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useState</span> <span class="p">}</span> <span class="nx">from</span> <span class="s2">&#34;react&#34;</span>
<span class="kr">import</span> <span class="p">{</span> <span class="nx">createContainer</span> <span class="p">}</span> <span class="nx">from</span> <span class="s2">&#34;unstated-next&#34;</span>
<span class="kr">import</span> <span class="p">{</span> <span class="nx">render</span> <span class="p">}</span> <span class="nx">from</span> <span class="s2">&#34;react-dom&#34;</span>

<span class="kd">function</span> <span class="nx">useCounter</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="p">[</span><span class="nx">count</span><span class="p">,</span> <span class="nx">setCount</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
  <span class="kd">let</span> <span class="nx">decrement</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="nx">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
  <span class="kd">let</span> <span class="nx">increment</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="nx">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
  <span class="k">return</span> <span class="p">{</span> <span class="nx">count</span><span class="p">,</span> <span class="nx">decrement</span><span class="p">,</span> <span class="nx">increment</span> <span class="p">}</span>
<span class="p">}</span>

<span class="kd">let</span> <span class="nx">Counter</span> <span class="o">=</span> <span class="nx">createContainer</span><span class="p">(</span><span class="nx">useCounter</span><span class="p">)</span>

<span class="kd">function</span> <span class="nx">CounterDisplay</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="nx">counter</span> <span class="o">=</span> <span class="nx">Counter</span><span class="p">.</span><span class="nx">useContainer</span><span class="p">()</span>
  <span class="k">return</span> <span class="p">(</span>
    <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span>
      <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">decrement</span><span class="p">}</span><span class="o">&gt;-&lt;</span><span class="err">/button&gt;</span>
      <span class="o">&lt;</span><span class="nx">span</span><span class="o">&gt;</span><span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">count</span><span class="p">}</span><span class="o">&lt;</span><span class="err">/span&gt;</span>
      <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">increment</span><span class="p">}</span><span class="o">&gt;+&lt;</span><span class="err">/button&gt;</span>
    <span class="o">&lt;</span><span class="err">/div&gt;</span>
  <span class="p">)</span>
<span class="p">}</span>

<span class="kd">function</span> <span class="nx">App</span><span class="p">()</span> <span class="p">{</span>
  <span class="k">return</span> <span class="p">(</span>
    <span class="o">&lt;</span><span class="nx">Counter</span><span class="p">.</span><span class="nx">Provider</span><span class="o">&gt;</span>
      <span class="o">&lt;</span><span class="nx">CounterDisplay</span> <span class="o">/&gt;</span>
      <span class="o">&lt;</span><span class="nx">CounterDisplay</span> <span class="o">/&gt;</span>
    <span class="o">&lt;</span><span class="err">/Counter.Provider&gt;</span>
  <span class="p">)</span>
<span class="p">}</span>

<span class="nx">render</span><span class="p">(</span><span class="o">&lt;</span><span class="nx">App</span> <span class="o">/&gt;</span><span class="p">,</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s2">&#34;root&#34;</span><span class="p">))</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="api">API</h2>
<h3 id="createcontainerusehook"><code>createContainer(useHook)</code></h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-js" data-lang="js"><span class="kr">import</span> <span class="p">{</span> <span class="nx">createContainer</span> <span class="p">}</span> <span class="nx">from</span> <span class="s2">&#34;unstated-next&#34;</span>

<span class="kd">function</span> <span class="nx">useCustomHook</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="p">[</span><span class="nx">value</span><span class="p">,</span> <span class="nx">setInput</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">()</span>
  <span class="kd">let</span> <span class="nx">onChange</span> <span class="o">=</span> <span class="nx">e</span> <span class="p">=&gt;</span> <span class="nx">setValue</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">currentTarget</span><span class="p">.</span><span class="nx">value</span><span class="p">)</span>
  <span class="k">return</span> <span class="p">{</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">onChange</span> <span class="p">}</span>
<span class="p">}</span>

<span class="kd">let</span> <span class="nx">Container</span> <span class="o">=</span> <span class="nx">createContainer</span><span class="p">(</span><span class="nx">useCustomHook</span><span class="p">)</span>
<span class="c1">// Container === { Provider, useContainer }
</span></code></pre></td></tr></table>
</div>
</div><h3 id="containerprovider"><code>&lt;Container.Provider&gt;</code></h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-js" data-lang="js"><span class="kd">function</span> <span class="nx">ParentComponent</span><span class="p">()</span> <span class="p">{</span>
  <span class="k">return</span> <span class="p">(</span>
    <span class="o">&lt;</span><span class="nx">Container</span><span class="p">.</span><span class="nx">Provider</span><span class="o">&gt;</span>
      <span class="o">&lt;</span><span class="nx">ChildComponent</span> <span class="o">/&gt;</span>
    <span class="o">&lt;</span><span class="err">/Container.Provider&gt;</span>
  <span class="p">)</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="containerusecontainer"><code>Container.useContainer()</code></h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-js" data-lang="js"><span class="kd">function</span> <span class="nx">ChildComponent</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="nx">input</span> <span class="o">=</span> <span class="nx">Container</span><span class="p">.</span><span class="nx">useContainer</span><span class="p">()</span>
  <span class="k">return</span> <span class="o">&lt;</span><span class="nx">input</span> <span class="nx">value</span><span class="o">=</span><span class="p">{</span><span class="nx">input</span><span class="p">.</span><span class="nx">value</span><span class="p">}</span> <span class="nx">onChange</span><span class="o">=</span><span class="p">{</span><span class="nx">input</span><span class="p">.</span><span class="nx">onChange</span><span class="p">}</span> <span class="o">/&gt;</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="usecontainercontainer"><code>useContainer(Container)</code></h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-js" data-lang="js"><span class="kr">import</span> <span class="p">{</span> <span class="nx">useContainer</span> <span class="p">}</span> <span class="nx">from</span> <span class="s2">&#34;unstated-next&#34;</span>

<span class="kd">function</span> <span class="nx">ChildComponent</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="nx">input</span> <span class="o">=</span> <span class="nx">useContainer</span><span class="p">(</span><span class="nx">Container</span><span class="p">)</span>
  <span class="k">return</span> <span class="o">&lt;</span><span class="nx">input</span> <span class="nx">value</span><span class="o">=</span><span class="p">{</span><span class="nx">input</span><span class="p">.</span><span class="nx">value</span><span class="p">}</span> <span class="nx">onChange</span><span class="o">=</span><span class="p">{</span><span class="nx">input</span><span class="p">.</span><span class="nx">onChange</span><span class="p">}</span> <span class="o">/&gt;</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="指南">指南</h2>
<p>如果你以前从未使用过 React Hooks，我不建议你往下看，请先阅读 [React 官网的 React Hooks 文档]（https://reactjs.org/docs/hooks-intro.html）。</p>
<p>首先，使用 React Hooks，你可以创建这样一个组件：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-js" data-lang="js"><span class="kd">function</span> <span class="nx">CounterDisplay</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="p">[</span><span class="nx">count</span><span class="p">,</span> <span class="nx">setCount</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
  <span class="kd">let</span> <span class="nx">decrement</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="nx">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
  <span class="kd">let</span> <span class="nx">increment</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="nx">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
  <span class="k">return</span> <span class="p">(</span>
    <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span>
      <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">decrement</span><span class="p">}</span><span class="o">&gt;-&lt;</span><span class="err">/button&gt;</span>
      <span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="nx">You</span> <span class="nx">clicked</span> <span class="p">{</span><span class="nx">count</span><span class="p">}</span> <span class="nx">times</span><span class="o">&lt;</span><span class="err">/p&gt;</span>
      <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">increment</span><span class="p">}</span><span class="o">&gt;+&lt;</span><span class="err">/button&gt;</span>
    <span class="o">&lt;</span><span class="err">/div&gt;</span>
  <span class="p">)</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>然后，如果你想共享组件的逻辑，你可以把它写在组件外面，自定义一个 hook:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-js" data-lang="js"><span class="kd">function</span> <span class="nx">useCounter</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="p">[</span><span class="nx">count</span><span class="p">,</span> <span class="nx">setCount</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
  <span class="kd">let</span> <span class="nx">decrement</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="nx">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
  <span class="kd">let</span> <span class="nx">increment</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="nx">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
  <span class="k">return</span> <span class="p">{</span> <span class="nx">count</span><span class="p">,</span> <span class="nx">decrement</span><span class="p">,</span> <span class="nx">increment</span> <span class="p">}</span>
<span class="p">}</span>

<span class="kd">function</span> <span class="nx">CounterDisplay</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="nx">counter</span> <span class="o">=</span> <span class="nx">useCounter</span><span class="p">()</span>
  <span class="k">return</span> <span class="p">(</span>
    <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span>
      <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">decrement</span><span class="p">}</span><span class="o">&gt;-&lt;</span><span class="err">/button&gt;</span>
      <span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="nx">You</span> <span class="nx">clicked</span> <span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">count</span><span class="p">}</span> <span class="nx">times</span><span class="o">&lt;</span><span class="err">/p&gt;</span>
      <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">increment</span><span class="p">}</span><span class="o">&gt;+&lt;</span><span class="err">/button&gt;</span>
    <span class="o">&lt;</span><span class="err">/div&gt;</span>
  <span class="p">)</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>但是，除了共享逻辑之外，你还想共享状态，你会怎么做呢？</p>
<p>这个时候，context 就发挥了作用：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-js" data-lang="js"><span class="kd">function</span> <span class="nx">useCounter</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="p">[</span><span class="nx">count</span><span class="p">,</span> <span class="nx">setCount</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
  <span class="kd">let</span> <span class="nx">decrement</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="nx">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
  <span class="kd">let</span> <span class="nx">increment</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="nx">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
  <span class="k">return</span> <span class="p">{</span> <span class="nx">count</span><span class="p">,</span> <span class="nx">decrement</span><span class="p">,</span> <span class="nx">increment</span> <span class="p">}</span>
<span class="p">}</span>

<span class="kd">let</span> <span class="nx">Counter</span> <span class="o">=</span> <span class="nx">createContext</span><span class="p">(</span><span class="kc">null</span><span class="p">)</span>

<span class="kd">function</span> <span class="nx">CounterDisplay</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="nx">counter</span> <span class="o">=</span> <span class="nx">useContext</span><span class="p">(</span><span class="nx">Counter</span><span class="p">)</span>
  <span class="k">return</span> <span class="p">(</span>
    <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span>
      <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">decrement</span><span class="p">}</span><span class="o">&gt;-&lt;</span><span class="err">/button&gt;</span>
      <span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="nx">You</span> <span class="nx">clicked</span> <span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">count</span><span class="p">}</span> <span class="nx">times</span><span class="o">&lt;</span><span class="err">/p&gt;</span>
      <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">increment</span><span class="p">}</span><span class="o">&gt;+&lt;</span><span class="err">/button&gt;</span>
    <span class="o">&lt;</span><span class="err">/div&gt;</span>
  <span class="p">)</span>
<span class="p">}</span>

<span class="kd">function</span> <span class="nx">App</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="nx">counter</span> <span class="o">=</span> <span class="nx">useCounter</span><span class="p">()</span>
  <span class="k">return</span> <span class="p">(</span>
    <span class="o">&lt;</span><span class="nx">Counter</span><span class="p">.</span><span class="nx">Provider</span> <span class="nx">value</span><span class="o">=</span><span class="p">{</span><span class="nx">counter</span><span class="p">}</span><span class="o">&gt;</span>
      <span class="o">&lt;</span><span class="nx">CounterDisplay</span> <span class="o">/&gt;</span>
      <span class="o">&lt;</span><span class="nx">CounterDisplay</span> <span class="o">/&gt;</span>
    <span class="o">&lt;</span><span class="err">/Counter.Provider&gt;</span>
  <span class="p">)</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>这很棒，也很完美，更多人应该编写这样的代码。</p>
<p>但有时我们需要更多的结构和特定的 API 设计才能使其始终保持正确。</p>
<p>通过引入 <code>createContainer()</code> 函数，你可以将自定义 hooks 作为 containers，并且定义明确的 API，防止错误使用。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-js" data-lang="js"><span class="kr">import</span> <span class="p">{</span> <span class="nx">createContainer</span> <span class="p">}</span> <span class="nx">from</span> <span class="s2">&#34;unstated-next&#34;</span>

<span class="kd">function</span> <span class="nx">useCounter</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="p">[</span><span class="nx">count</span><span class="p">,</span> <span class="nx">setCount</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
  <span class="kd">let</span> <span class="nx">decrement</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="nx">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
  <span class="kd">let</span> <span class="nx">increment</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="nx">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
  <span class="k">return</span> <span class="p">{</span> <span class="nx">count</span><span class="p">,</span> <span class="nx">decrement</span><span class="p">,</span> <span class="nx">increment</span> <span class="p">}</span>
<span class="p">}</span>

<span class="kd">let</span> <span class="nx">Counter</span> <span class="o">=</span> <span class="nx">createContainer</span><span class="p">(</span><span class="nx">useCounter</span><span class="p">)</span>

<span class="kd">function</span> <span class="nx">CounterDisplay</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="nx">counter</span> <span class="o">=</span> <span class="nx">Counter</span><span class="p">.</span><span class="nx">useContainer</span><span class="p">()</span>
  <span class="k">return</span> <span class="p">(</span>
    <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span>
      <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">decrement</span><span class="p">}</span><span class="o">&gt;-&lt;</span><span class="err">/button&gt;</span>
      <span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="nx">You</span> <span class="nx">clicked</span> <span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">count</span><span class="p">}</span> <span class="nx">times</span><span class="o">&lt;</span><span class="err">/p&gt;</span>
      <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">increment</span><span class="p">}</span><span class="o">&gt;+&lt;</span><span class="err">/button&gt;</span>
    <span class="o">&lt;</span><span class="err">/div&gt;</span>
  <span class="p">)</span>
<span class="p">}</span>

<span class="kd">function</span> <span class="nx">App</span><span class="p">()</span> <span class="p">{</span>
  <span class="k">return</span> <span class="p">(</span>
    <span class="o">&lt;</span><span class="nx">Counter</span><span class="p">.</span><span class="nx">Provider</span><span class="o">&gt;</span>
      <span class="o">&lt;</span><span class="nx">CounterDisplay</span> <span class="o">/&gt;</span>
      <span class="o">&lt;</span><span class="nx">CounterDisplay</span> <span class="o">/&gt;</span>
    <span class="o">&lt;</span><span class="err">/Counter.Provider&gt;</span>
  <span class="p">)</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>下面是前后的代码对比：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-diff" data-lang="diff"><span class="gd">- import { createContext, useContext } from &#34;react&#34;
</span><span class="gd"></span><span class="gi">+ import { createContainer } from &#34;unstated-next&#34;
</span><span class="gi"></span>
  function useCounter() {
    ...
  }

<span class="gd">- let Counter = createContext(null)
</span><span class="gd"></span><span class="gi">+ let Counter = createContainer(useCounter)
</span><span class="gi"></span>
  function CounterDisplay() {
<span class="gd">-   let counter = useContext(Counter)
</span><span class="gd"></span><span class="gi">+   let counter = Counter.useContainer()
</span><span class="gi"></span>    return (
      &lt;div&gt;
        ...
      &lt;/div&gt;
    )
  }

  function App() {
<span class="gd">-   let counter = useCounter()
</span><span class="gd"></span>    return (
<span class="gd">-     &lt;Counter.Provider value={counter}&gt;
</span><span class="gd"></span><span class="gi">+     &lt;Counter.Provider&gt;
</span><span class="gi"></span>        &lt;CounterDisplay /&gt;
        &lt;CounterDisplay /&gt;
      &lt;/Counter.Provider&gt;
    )
  }
</code></pre></td></tr></table>
</div>
</div><p>如果你正在使用 TypeScript（我鼓励你了解更多关于它的信息），这也有助于 TypeScript 的内置推断做得更好。只要你的自定义 hook 类型是完善的，那么类型都会自动推断。</p>
<h2 id="提示">提示</h2>
<h3 id="提示-1-组合-containers">提示 #1: 组合 Containers</h3>
<p>因为我们只使用了自定义 React hooks，所以可以在其他 hooks 内部组合 containers。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-js" data-lang="js"><span class="kd">function</span> <span class="nx">useCounter</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="p">[</span><span class="nx">count</span><span class="p">,</span> <span class="nx">setCount</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
  <span class="kd">let</span> <span class="nx">decrement</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="nx">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
  <span class="kd">let</span> <span class="nx">increment</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="nx">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
  <span class="k">return</span> <span class="p">{</span> <span class="nx">count</span><span class="p">,</span> <span class="nx">decrement</span><span class="p">,</span> <span class="nx">increment</span><span class="p">,</span> <span class="nx">setCount</span> <span class="p">}</span>
<span class="p">}</span>

<span class="kd">let</span> <span class="nx">Counter</span> <span class="o">=</span> <span class="nx">createContainer</span><span class="p">(</span><span class="nx">useCounter</span><span class="p">)</span>

<span class="kd">function</span> <span class="nx">useResettableCounter</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="nx">counter</span> <span class="o">=</span> <span class="nx">Counter</span><span class="p">.</span><span class="nx">useContainer</span><span class="p">()</span>
  <span class="kd">let</span> <span class="nx">reset</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="nx">counter</span><span class="p">.</span><span class="nx">setCount</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
  <span class="k">return</span> <span class="p">{</span> <span class="p">...</span><span class="nx">counter</span><span class="p">,</span> <span class="nx">reset</span> <span class="p">}</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="提示-2-保持-containers-很小">提示 #2: 保持 Containers 很小</h3>
<p>这对于保持 containers 小而集中非常有用。 如果你想在 containers 中对代码进行逻辑拆分，那么这一点非常重要。只需将它们移动到自己的 hooks 中，仅保存 containers 的状态即可。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-js" data-lang="js"><span class="kd">function</span> <span class="nx">useCount</span><span class="p">()</span> <span class="p">{</span>
  <span class="k">return</span> <span class="nx">useState</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="p">}</span>

<span class="kd">let</span> <span class="nx">Count</span> <span class="o">=</span> <span class="nx">createContainer</span><span class="p">(</span><span class="nx">useCount</span><span class="p">)</span>

<span class="kd">function</span> <span class="nx">useCounter</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="p">[</span><span class="nx">count</span><span class="p">,</span> <span class="nx">setCount</span><span class="p">]</span> <span class="o">=</span> <span class="nx">Count</span><span class="p">.</span><span class="nx">useContainer</span><span class="p">()</span>
  <span class="kd">let</span> <span class="nx">decrement</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="nx">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
  <span class="kd">let</span> <span class="nx">increment</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="nx">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
  <span class="kd">let</span> <span class="nx">reset</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="nx">setCount</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
  <span class="k">return</span> <span class="p">{</span> <span class="nx">count</span><span class="p">,</span> <span class="nx">decrement</span><span class="p">,</span> <span class="nx">increment</span><span class="p">,</span> <span class="nx">reset</span> <span class="p">}</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="提示-3-优化组件">提示 #3: 优化组件</h3>
<p><code>unstated-next</code> 无需优化。所有你要做的优化，都是标准的 React 优化。</p>
<h4 id="1-通过拆分组件来优化耗时的子树">1) 通过拆分组件来优化耗时的子树</h4>
<p><strong>优化前:</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-js" data-lang="js"><span class="kd">function</span> <span class="nx">CounterDisplay</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="nx">counter</span> <span class="o">=</span> <span class="nx">Counter</span><span class="p">.</span><span class="nx">useContainer</span><span class="p">()</span>
  <span class="k">return</span> <span class="p">(</span>
    <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span>
      <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">decrement</span><span class="p">}</span><span class="o">&gt;-&lt;</span><span class="err">/button&gt;</span>
      <span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="nx">You</span> <span class="nx">clicked</span> <span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">count</span><span class="p">}</span> <span class="nx">times</span><span class="o">&lt;</span><span class="err">/p&gt;</span>
      <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">increment</span><span class="p">}</span><span class="o">&gt;+&lt;</span><span class="err">/button&gt;</span>
      <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span>
        <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span>
          <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span>
            <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span><span class="nx">SUPER</span> <span class="nx">EXPENSIVE</span> <span class="nx">RENDERING</span> <span class="nx">STUFF</span><span class="o">&lt;</span><span class="err">/div&gt;</span>
          <span class="o">&lt;</span><span class="err">/div&gt;</span>
        <span class="o">&lt;</span><span class="err">/div&gt;</span>
      <span class="o">&lt;</span><span class="err">/div&gt;</span>
    <span class="o">&lt;</span><span class="err">/div&gt;</span>
  <span class="p">)</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>优化后:</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-js" data-lang="js"><span class="kd">function</span> <span class="nx">ExpensiveComponent</span><span class="p">()</span> <span class="p">{</span>
  <span class="k">return</span> <span class="p">(</span>
    <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span>
      <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span>
        <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span>
          <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span><span class="nx">SUPER</span> <span class="nx">EXPENSIVE</span> <span class="nx">RENDERING</span> <span class="nx">STUFF</span><span class="o">&lt;</span><span class="err">/div&gt;</span>
        <span class="o">&lt;</span><span class="err">/div&gt;</span>
      <span class="o">&lt;</span><span class="err">/div&gt;</span>
    <span class="o">&lt;</span><span class="err">/div&gt;</span>
  <span class="p">)</span>
<span class="p">}</span>

<span class="kd">function</span> <span class="nx">CounterDisplay</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="nx">counter</span> <span class="o">=</span> <span class="nx">Counter</span><span class="p">.</span><span class="nx">useContainer</span><span class="p">()</span>
  <span class="k">return</span> <span class="p">(</span>
    <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span>
      <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">decrement</span><span class="p">}</span><span class="o">&gt;-&lt;</span><span class="err">/button&gt;</span>
      <span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="nx">You</span> <span class="nx">clicked</span> <span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">count</span><span class="p">}</span> <span class="nx">times</span><span class="o">&lt;</span><span class="err">/p&gt;</span>
      <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">increment</span><span class="p">}</span><span class="o">&gt;+&lt;</span><span class="err">/button&gt;</span>
      <span class="o">&lt;</span><span class="nx">ExpensiveComponent</span> <span class="o">/&gt;</span>
    <span class="o">&lt;</span><span class="err">/div&gt;</span>
  <span class="p">)</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="2-使用-usememo-优化耗时的操作">2) 使用 useMemo() 优化耗时的操作</h4>
<p><strong>优化前:</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-js" data-lang="js"><span class="kd">function</span> <span class="nx">CounterDisplay</span><span class="p">(</span><span class="nx">props</span><span class="p">)</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="nx">counter</span> <span class="o">=</span> <span class="nx">Counter</span><span class="p">.</span><span class="nx">useContainer</span><span class="p">()</span>

  <span class="c1">// 每次 `counter` 改变都要重新计算这个值，非常耗时
</span><span class="c1"></span>  <span class="kd">let</span> <span class="nx">expensiveValue</span> <span class="o">=</span> <span class="nx">expensiveComputation</span><span class="p">(</span><span class="nx">props</span><span class="p">.</span><span class="nx">input</span><span class="p">)</span>

  <span class="k">return</span> <span class="p">(</span>
    <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span>
      <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">decrement</span><span class="p">}</span><span class="o">&gt;-&lt;</span><span class="err">/button&gt;</span>
      <span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="nx">You</span> <span class="nx">clicked</span> <span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">count</span><span class="p">}</span> <span class="nx">times</span><span class="o">&lt;</span><span class="err">/p&gt;</span>
      <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">increment</span><span class="p">}</span><span class="o">&gt;+&lt;</span><span class="err">/button&gt;</span>
    <span class="o">&lt;</span><span class="err">/div&gt;</span>
  <span class="p">)</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>优化后:</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-js" data-lang="js"><span class="kd">function</span> <span class="nx">CounterDisplay</span><span class="p">(</span><span class="nx">props</span><span class="p">)</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="nx">counter</span> <span class="o">=</span> <span class="nx">Counter</span><span class="p">.</span><span class="nx">useContainer</span><span class="p">()</span>

  <span class="c1">// 仅在输入更改时重新计算这个值
</span><span class="c1"></span>  <span class="kd">let</span> <span class="nx">expensiveValue</span> <span class="o">=</span> <span class="nx">useMemo</span><span class="p">(()</span> <span class="p">=&gt;</span> <span class="p">{</span>
    <span class="k">return</span> <span class="nx">expensiveComputation</span><span class="p">(</span><span class="nx">props</span><span class="p">.</span><span class="nx">input</span><span class="p">)</span>
  <span class="p">},</span> <span class="p">[</span><span class="nx">props</span><span class="p">.</span><span class="nx">input</span><span class="p">])</span>

  <span class="k">return</span> <span class="p">(</span>
    <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span>
      <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">decrement</span><span class="p">}</span><span class="o">&gt;-&lt;</span><span class="err">/button&gt;</span>
      <span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="nx">You</span> <span class="nx">clicked</span> <span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">count</span><span class="p">}</span> <span class="nx">times</span><span class="o">&lt;</span><span class="err">/p&gt;</span>
      <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">increment</span><span class="p">}</span><span class="o">&gt;+&lt;</span><span class="err">/button&gt;</span>
    <span class="o">&lt;</span><span class="err">/div&gt;</span>
  <span class="p">)</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="3-使用-reactmemousecallback-减少重新渲染次数">3) 使用 React.memo()、useCallback() 减少重新渲染次数</h4>
<p><strong>优化前:</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-js" data-lang="js"><span class="kd">function</span> <span class="nx">useCounter</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="p">[</span><span class="nx">count</span><span class="p">,</span> <span class="nx">setCount</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
  <span class="kd">let</span> <span class="nx">decrement</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="nx">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
  <span class="kd">let</span> <span class="nx">increment</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="nx">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
  <span class="k">return</span> <span class="p">{</span> <span class="nx">count</span><span class="p">,</span> <span class="nx">decrement</span><span class="p">,</span> <span class="nx">increment</span> <span class="p">}</span>
<span class="p">}</span>

<span class="kd">let</span> <span class="nx">Counter</span> <span class="o">=</span> <span class="nx">createContainer</span><span class="p">(</span><span class="nx">useCounter</span><span class="p">)</span>

<span class="kd">function</span> <span class="nx">CounterDisplay</span><span class="p">(</span><span class="nx">props</span><span class="p">)</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="nx">counter</span> <span class="o">=</span> <span class="nx">Counter</span><span class="p">.</span><span class="nx">useContainer</span><span class="p">()</span>
  <span class="k">return</span> <span class="p">(</span>
    <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span>
      <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">decrement</span><span class="p">}</span><span class="o">&gt;-&lt;</span><span class="err">/button&gt;</span>
      <span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="nx">You</span> <span class="nx">clicked</span> <span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">count</span><span class="p">}</span> <span class="nx">times</span><span class="o">&lt;</span><span class="err">/p&gt;</span>
      <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">counter</span><span class="p">.</span><span class="nx">increment</span><span class="p">}</span><span class="o">&gt;+&lt;</span><span class="err">/button&gt;</span>
    <span class="o">&lt;</span><span class="err">/div&gt;</span>
  <span class="p">)</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>优化后:</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-js" data-lang="js"><span class="kd">function</span> <span class="nx">useCounter</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="p">[</span><span class="nx">count</span><span class="p">,</span> <span class="nx">setCount</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
  <span class="kd">let</span> <span class="nx">decrement</span> <span class="o">=</span> <span class="nx">useCallback</span><span class="p">(()</span> <span class="p">=&gt;</span> <span class="nx">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">-</span> <span class="mi">1</span><span class="p">),</span> <span class="p">[</span><span class="nx">count</span><span class="p">])</span>
  <span class="kd">let</span> <span class="nx">increment</span> <span class="o">=</span> <span class="nx">useCallback</span><span class="p">(()</span> <span class="p">=&gt;</span> <span class="nx">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">+</span> <span class="mi">1</span><span class="p">),</span> <span class="p">[</span><span class="nx">count</span><span class="p">])</span>
  <span class="k">return</span> <span class="p">{</span> <span class="nx">count</span><span class="p">,</span> <span class="nx">decrement</span><span class="p">,</span> <span class="nx">increment</span> <span class="p">}</span>
<span class="p">}</span>

<span class="kd">let</span> <span class="nx">Counter</span> <span class="o">=</span> <span class="nx">createContainer</span><span class="p">(</span><span class="nx">useCounter</span><span class="p">)</span>

<span class="kd">let</span> <span class="nx">CounterDisplayInner</span> <span class="o">=</span> <span class="nx">React</span><span class="p">.</span><span class="nx">memo</span><span class="p">(</span><span class="nx">props</span> <span class="p">=&gt;</span> <span class="p">{</span>
  <span class="k">return</span> <span class="p">(</span>
    <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span>
      <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">props</span><span class="p">.</span><span class="nx">decrement</span><span class="p">}</span><span class="o">&gt;-&lt;</span><span class="err">/button&gt;</span>
      <span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="nx">You</span> <span class="nx">clicked</span> <span class="p">{</span><span class="nx">props</span><span class="p">.</span><span class="nx">count</span><span class="p">}</span> <span class="nx">times</span><span class="o">&lt;</span><span class="err">/p&gt;</span>
      <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">props</span><span class="p">.</span><span class="nx">increment</span><span class="p">}</span><span class="o">&gt;+&lt;</span><span class="err">/button&gt;</span>
    <span class="o">&lt;</span><span class="err">/div&gt;</span>
  <span class="p">)</span>
<span class="p">})</span>

<span class="kd">function</span> <span class="nx">CounterDisplay</span><span class="p">(</span><span class="nx">props</span><span class="p">)</span> <span class="p">{</span>
  <span class="kd">let</span> <span class="nx">counter</span> <span class="o">=</span> <span class="nx">Counter</span><span class="p">.</span><span class="nx">useContainer</span><span class="p">()</span>
  <span class="k">return</span> <span class="o">&lt;</span><span class="nx">CounterDisplayInner</span> <span class="p">{...</span><span class="nx">counter</span><span class="p">}</span> <span class="o">/&gt;</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="与-unstated-的关系">与 Unstated 的关系</h2>
<p>我认为这个库是 <a href="https://github.com/jamiebuilds/unstated">Unstated</a> 精神的继承者。因为我相信 React 在状态管理方面已经非常出色，唯一缺少的就是轻松共享状态和逻辑，所以我创建了 Unstated 。我创建的 Unstated 是 React 共享状态和逻辑的 <strong>最小</strong> 解决方案。</p>
<p>然而，使用 Hooks，React 在共享状态和逻辑方面可以做得更好。我甚至认为 Unstated 成为了不必要的抽象。</p>
<p><strong>但是</strong>，我认为很多开发人员都在努力了解如何使用 React Hooks 共享状态和逻辑，从而实现应用程序共享状态。这可能只是文档和社区动力的问题，但我认为一个新的 API 可以弥补这种心理差距。</p>
<p>这个 API 就是 Unstated Next。 它不是 <strong>React 中共享状态和逻辑的最小 API</strong>，而是<strong>用于理解如何在 React 中共享状态和逻辑的最小 API</strong>。</p>
<p>我一直给 React 站队。我希望 React 可以赢。 我希望社区放弃像 Redux 这样的状态管理库，并找到使用 React 内置工具链的更好方法。</p>
<p>如果你不想使用 Unstated，你只想使用 React 本身，我非常鼓励你这么做。 写关于它的博客文章！ 讨论它！ 在社区中传播你的知识。</p>
<h2 id="从-unstated-迁移">从 <code>unstated</code> 迁移</h2>
<p>我故意将其发布为单独的包，因为它是对原有 API 的完全重写。 这样，你可以逐步安装和迁移。</p>
<p>请向我提供有关该迁移过程的反馈，因为在接下来的几个月里，我希望得到这些反馈并做以下两件事：</p>
<ul>
<li>确保 <code>unstated-next</code> 满足 <code>unstated</code> 使用者的所有需求。</li>
<li>确保 <code>unstated</code> 使用者的代码可以完整地迁移到 <code>unstated-next</code>。</li>
</ul>
<p>我可以将 API 新增到两者的任意一个仓库中，从而使开发人员工作得更轻松。 对于 <code>unstated-next</code>，我将保证新增的 API 尽可能小，同时，我也会尽量保持库很小。</p>
<p>未来，我可能会将 <code>unstated-next</code> 合并为 <code>unstated</code> 的主要版本。 <code>unstated-next</code> 仍然存在，这样你就可以安装 <code>unstated@2</code> 和 <code>unstated-next</code>。 当你完成迁移后，你可以更新到 <code>unstated@3</code> ，同时删除 <code>unstated-next</code>（确保更新你所有的引入，这应该只是一个查找和替换的过程）。</p>
<p>尽管这是一个重大的 API 更改，我希望你尽可能轻松地完成此迁移。我正在使用最新的 React Hooks API ，为你进行优化，而不是使用原有的 <code>Unstated.Container</code> 代码。请随意提供有关如何做得更好的反馈。</p>
<blockquote>
<p>首发 <a href="https://nusr.github.io/">https://nusr.github.io/</a></p>
</blockquote>
  </div>

  <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">早起的年轻人</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2019-05-08 20:09
        
    </span>
  </p>
  
  <p class="copyright-item">
    <span class="item-title">许可协议</span>
    <span class="item-content"><a target="_blank" rel="license noopener" href="https://github.com/nusr/blog/blob/master/LICENSE">MIT</a></span>
  </p>
</div>
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    <label class="qr-code-image" for="reward">
        <img class="image" src="/images/wechat.jpg">
        <span>微信佛系打赏</span>
      </label>
    <label class="qr-code-image" for="reward">
        <img class="image" src="/images/alipay.jpg">
        <span>支付宝佛系打赏</span>
      </label>
  </div>
</div><footer class="post-footer">
    <div class="post-tags">
      <a href="/tags/React/">React</a>
      <a href="/tags/State-Management/">State Management</a>
      </div>
    <nav class="post-nav">
      <a class="prev" href="/post/2019/2019-05-16-hacker-laws-chinese/">
        <i class="iconfont icon-left"></i>
        <span class="prev-text nav-default">黑客不可不知的定律</span>
        <span class="prev-text nav-mobile">上一篇</span>
      </a>
      <a class="next" href="/post/2019/2019-05-04-utterances-introduce/">
        <span class="next-text nav-default">utterances 用做博客评论</span>
        <span class="next-text nav-mobile">下一篇</span>
        <i class="iconfont icon-right"></i>
      </a>
    </nav>
  </footer>
  
</article>
            </div>
            

        </div>
    </main>

    <footer id="footer" class="footer">
        <div class="social-links">
  <a href="852851198@qq.com" class="iconfont icon-email" title="email" rel="noopener" target="_blank" data-title="email"> </a>
  <a href="https://github.com/ITmxs" class="iconfont icon-github" title="github" rel="noopener" target="_blank" data-title="github"> </a>
  <a href="https://juejin.cn/user/3843548384077192" class="iconfont icon-juejin" title="juejin" rel="noopener" target="_blank" data-title="juejin"> </a>
  <a href="https://www.zhihu.com/people/yimi-yang-guang-96-65" class="iconfont icon-zhihu" title="zhihu" rel="noopener" target="_blank" data-title="zhihu"> </a>
  <a href="https://space.bilibili.com/480883651" class="iconfont icon-bilibili" title="bilibili" rel="noopener" target="_blank" data-title="bilibili"> </a>
  <a href="https://luckly.work/index.xml" type="application/rss+xml" rel="noopener" target="_blank" class="iconfont icon-rss" title="rss"></a>


</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 -
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  <div class="busuanzi-footer">
    <span id="busuanzi_container_site_pv"> 本站总访问量 <span id="busuanzi_value_site_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次 </span>
    <span class="division">|</span>
    <span id="busuanzi_container_site_uv"> 本站总访客数 <span id="busuanzi_value_site_uv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 人 </span>
  </div>

  <span class="copyright-year">
    &copy;
    2020 -
    2021
    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">早起的年轻人</span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
        <i class="iconfont icon-up"></i>
    </div>
</div>

  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/timeago.js@3.0.2/dist/timeago.min.js" integrity="sha256-jwCP0NAdCBloaIWTWHmW4i3snUNMHUNO+jr9rYd2iOI=" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/timeago.js@3.0.2/dist/timeago.locales.min.js" integrity="sha256-ZwofwC1Lf/faQCzN7nZtfijVV6hSwxjQMwXL4gn9qU8=" crossorigin="anonymous"></script>
  <script><!-- NOTE: timeago.js uses the language code format like "zh_CN" (underscore and case sensitive) -->
    var languageCode = "zh-CN".replace(/-/g, '_').replace(/_(.*)/, function ($0, $1) {return $0.replace($1, $1.toUpperCase());});
    timeago().render(document.querySelectorAll('.timeago'), languageCode);
    timeago.cancel();  
  </script>
<script type="text/javascript" src="/dist/even.47f727f4.min.js"></script>


<script type="application/javascript">
var doNotTrack = false;
if (!doNotTrack) {
	window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
	ga('create', 'UA-138883536-1', 'auto');
	ga('set', 'anonymizeIp', true);
	ga('send', 'pageview');
}
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>







</body>
</html>
